package com.sina.weibo.view;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.widget.FaceManager;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class EmotionView extends GridView{
	private Context context = null;

	public EmotionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub

		this.context = context;
	}

	private void switchEmotionGridBoard(boolean bShow) {
		if (bShow) {
			setVisibility(View.VISIBLE);
			
			ImageAdapter imAdapter = new ImageAdapter(context);
			
			setAdapter(imAdapter);
		} else {
			setVisibility(View.GONE);
		}
	}
	
	public class ImageAdapter extends BaseAdapter { 
	    public ImageAdapter(Context c) { 
	        mContext = c; 
	    } 
	 
	    public int getCount() { 
	        return FaceManager.getInstance().getFaceCount(); 
	    } 
	 
	    public Object getItem(int position) { 
	        return position; 
	    } 
	 
	    public long getItemId(int position) { 
	        return position; 
	    } 
	 
	    public View getView(int position, View convertView, ViewGroup parent) { 
	        ImageView imageView; 
	        if (convertView == null) { 
	            imageView = new ImageView(mContext); 
	            imageView.setLayoutParams(new GridView.LayoutParams(60, 60));//设置ImageView宽高 
	            imageView.setAdjustViewBounds(false); 
	            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
	            imageView.setPadding(20, 15, 20, 15); 
	        } else { 
	            imageView = (ImageView) convertView; 
	        } 
	 
	        imageView.setImageResource(FaceManager.getInstance().getFaceDrawableRes(position)); 
	 
	        return imageView; 
	    } 
	 
	    private Context mContext; 
	} 

	private void switchSoftKeybord(boolean bShow) {
		InputMethodManager imm = (InputMethodManager) ((Activity) context)
				.getSystemService(Activity.INPUT_METHOD_SERVICE);
		View view = ((Activity) context).getCurrentFocus();
		if (view != null) {
			if (!bShow) {
				imm.hideSoftInputFromWindow(view.getWindowToken(), 0);// 隐藏软键盘
			} else {
				imm.showSoftInput(view, 0); // 显示软键盘
			}
		} 
	}
	
	public void showEmotionBoard() {
		/// 隐藏键盘
		switchSoftKeybord(false);
		
		/// 显示表情
		switchEmotionGridBoard(true);
	}
	
	public void showKeyboard() {
		/// 隐藏表情
		switchEmotionGridBoard(false);
		
		/// 显示键盘
		switchSoftKeybord(true);
	}
	
	public void hide() {
		/// 隐藏表情
		switchEmotionGridBoard(false);
		
		/// 隐藏键盘
		switchSoftKeybord(false);
	}
}
